package com.sservice.service.service.impl;

import com.sservice.urp.dao.IRoleDao;
import com.sservice.urp.dao.IUserRoleDao;
import com.sservice.urp.entity.Role;
import com.sservice.urp.entity.UserRole;
import com.sservice.urp.vo.RolePermissionUnion;
import com.sservice.urp.vo.RoleVo;
import com.sservice.service.service.IRoleService;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IRoleDao dataDao;
    @Autowired
    private IUserRoleDao userRoleDao;

    @Override
    public List<Role> queryAll() {
        return dataDao.queryAll();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dataDao.deleteByPrimaryKey(id);
    }


    @Override
    public int insertSelective(Role arg) {

        int insert = dataDao.insertSelective(arg);

        return insert;
    }

    @Override
    public Role selectByPrimaryKey(Integer id) {
        return dataDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {

        return dataDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Role> getRolesByUserID(int userid) {
        return dataDao.getRolesByUserID(userid);
    }

    @Override
    public List<Role> getRoleList(Role role) {
        return dataDao.getRoleList(role);
    }

    @Override
    public List<RolePermissionUnion> queryAllRoleWithPermission() {
        return dataDao.queryAllRoleWithPermission();
    }

    @Override
    public List<Role> getUserRoleByUserId(int userid) {
        return dataDao.getRolesByUserID(userid);
    }

    /**
     * 判断角色下是否有用户
     *
     * @param userid
     * @return
     */
    @Override
    public int queryByRoleId(UserRole userid) {
        return userRoleDao.queryByRoleId(userid);
    }

    /**
     * 角色分页
     *
     * @param role
     * @return
     */
    @Override
    public ListResult queryByPage(RoleVo role) {
        List<RoleVo> list = dataDao.queryByPage(role);
        int total = dataDao.queryByCount(role);
        ListResult res = new ListResult(true, list);
        Pagination page = new Pagination(role.getPage(), total, role.getSize());
        res.setPage(page);
        return new ListResult(true, list);
    }

}
